package com.hhsjtest.bus_enter.viewholder

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import com.hhsjdesign.dynamicui.annoprocess.ABusinessViewHolder
import com.hhsjdesign.bus_enter.R
import com.hhsjtest.bus_enter.businessdata.Enter
import com.hhsjdesign.dynamicui.businessdata.IViewable
import com.hhsjdesign.dynamicui.reddot.RedDotFrameLayout
import com.hhsjdesign.dynamicui.reddot.RedDotLinearLayout
import com.hhsjdesign.dynamicui.utils.BusinessViewCreator
import com.hhsjdesign.dynamicui.core.property.viewdata.ItemViewData


@ABusinessViewHolder("enter")
class EnterViewHolder(itemWidgetType: String) : BusinessViewCreator<Enter>(itemWidgetType) {

    override fun createView(
        context: Context,
        itemWidgetType: String?,
        businessType: String?,
        itemViewData: ItemViewData?,
        parent: ViewGroup
    ): View = LayoutInflater.from(context)
        .inflate(R.layout.layout_enter_bg_left_icon_right_top_title_bottom_desc, parent, false)


    private fun listView(
        view: View,
        itemData: ItemViewData,
        map: MutableMap<String, View>?
    ): MutableMap<String, View> {

        var result: MutableMap<String, View>
        if (map == null) {
            result = HashMap<String, View>()
        } else {
            result = map
        }

        //1，自生
        result[view.getTag(R.id.segi_tag) as String] = view

        //2，child
        if (itemData.treeList == null) {
            return result
        }

        for (i in 0 until itemData.treeList!!.size) {
            listView(
                (view as ViewGroup).getChildAt(itemData.treeList!![i].index),
                itemData.treeList!![i],
                result
            )
        }
        return result
    }


    override fun transform(origin: Enter): IViewable<Enter> {
        return origin.toViewable() as IViewable<Enter>
    }

    override fun startActivity(context: Context, businessItemData: Enter?) {
        Toast.makeText(context, businessItemData!!.title, Toast.LENGTH_SHORT).show()
    }

    override fun bindDataAndEvent(
        view: View,
        viewable: IViewable<*>?,
        itemWidgetType: String?,
        businessType: String?,
        itemWidget: ItemViewData?
    ) {
        //        val map = listView(view,itemData,null)
        //        (map["text6"] as TextView).setText(viewable.title)
        val enter = viewable?.businessData as? Enter

        if (view is RedDotFrameLayout) {
            enter?.msgCode?.let {
                view.redDotTags.add(enter.msgCode!!)
            }
        } else if (view is RedDotLinearLayout) {
            enter?.msgCode?.let {
                view.redDotTags.add(enter.msgCode!!)
            }
        }

        view.findViewWithTag<TextView>("segi_title").text = enter?.title
        view.findViewWithTag<TextView>("segi_desc").text = enter?.desc
        view.setOnClickListener {
            startActivity(it.context, enter)
        }
    }

    override fun bindDataWithoutEvent(
        view: View,
        viewable: IViewable<*>?,
        itemWidgetType: String?,
        businessType: String?,
        itemWidget: ItemViewData?
    ) {
    }
}